/* eslint-disable react/prop-types */

import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import CastViewer from '../components/CastViewer';
import WorkEdit from '../components/WorkEdit';
import * as workAction from '../WorkAction';

@connect(mapStateToProps, buildActionDispatcher)
export default class WorkEditComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }

  componentDidMount() {
    const { params, workAction } = this.props;
    workAction.getMyWorkDetail(params.workId).then((work) => {
      this.setState({
        workInfo: work,
      });
    });
  }

  render() {
    const { workInfo } = this.state;
    const work = workInfo || {};
    return (
      <div id="work-detail" className="edit">
        <CastViewer workInfo={workInfo} {...this.props} />
        <WorkEdit
          work={work}
          categories={this.props.categories}
          myWorkAction={this.props.workAction}
          router={this.props.router}
          location={this.props.location}
        />
      </div>
    );
  }
}


function mapStateToProps(state) {
  const { Root } = state;
  const { isMobile, categories } = Root;
  return { isMobile, categories };
}

function buildActionDispatcher(dispatch) {
  return {
    workAction: bindActionCreators(workAction, dispatch),
  };
}
